package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbLmtrepairWorkdetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbRequestBo;
import com.ruoyi.tb.lmt.domain.vo.TbRequestVo;
import com.ruoyi.tb.lmt.domain.TbRequest;
import com.ruoyi.tb.lmt.mapper.TbRequestMapper;
import com.ruoyi.tb.lmt.service.ITbRequestService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 事件申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-18
 */
@RequiredArgsConstructor
@Service
public class TbRequestServiceImpl implements ITbRequestService {

    private final TbRequestMapper baseMapper;

    @Autowired
    private TbRequestMapper tbRequestMapper;

    /**
     * 查询事件申请
     */
    @Override
    public TbRequestVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询事件申请列表
     */
    @Override
    public TableDataInfo<TbRequestVo> queryPageList(TbRequestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbRequest> lqw = buildQueryWrapper(bo);
        Page<TbRequestVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询事件申请列表
     */
    @Override
    public List<TbRequestVo> queryList(TbRequestBo bo) {
        LambdaQueryWrapper<TbRequest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbRequest> buildQueryWrapper(TbRequestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbRequest> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLmtType()), TbRequest::getLmtType, bo.getLmtType());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtNumber()), TbRequest::getLmtNumber, bo.getLmtNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestType()), TbRequest::getRequestType, bo.getRequestType());
        lqw.like(StringUtils.isNotBlank(bo.getRequestName()), TbRequest::getRequestName, bo.getRequestName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkerTeam()), TbRequest::getWorkerTeam, bo.getWorkerTeam());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbRequest::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbRequest::getWorkGroup, bo.getWorkGroup());
        lqw.eq(bo.getWorkGroupId() != null, TbRequest::getWorkGroupId, bo.getWorkGroupId());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser()), TbRequest::getWorkUser, bo.getWorkUser());
        lqw.eq(bo.getWorkTime() != null, TbRequest::getWorkTime, bo.getWorkTime());
        lqw.eq(bo.getCreateTime() != null, TbRequest::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbRequest::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbRequest::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbRequest::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskUrl()), TbRequest::getTaskUrl, bo.getTaskUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestNeed()), TbRequest::getRequestNeed, bo.getRequestNeed());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestNeed2()), TbRequest::getRequestNeed2, bo.getRequestNeed2());
        return lqw;
    }

    /**
     * 新增事件申请
     */
    @Override
    public Boolean insertByBo(TbRequestBo bo) {
        TbRequest add = BeanUtil.toBean(bo, TbRequest.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改事件申请
     */
    @Override
    public Boolean updateByBo(TbRequestBo bo) {
        TbRequest update = BeanUtil.toBean(bo, TbRequest.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbRequest entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除事件申请
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TbRequestVo> selectRequestList(String requestType, String workerTeam, String lmtType, String lmtNumber, String workGroup, Date startTime, Date endTime, PageQuery pageQuery) {
        List<TbRequestVo> result = tbRequestMapper.selectRequestList(requestType, workerTeam, lmtType, lmtNumber, workGroup, startTime, endTime);
        //分页
        if (pageQuery.getPageSize()!= null && pageQuery.getPageNum()!= null) {
            List<TbRequestVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }
}
